package com.xtwy.repository;

import com.xtwy.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @作 者: 侯建军
 * @日 期: 2018/7/28 20:06
 * @描 述: 用户dao
 *  注意：字段名称与方法名的匹配
 * @公 司：山西迅腾伟业
 */
@Repository
public interface  UserRepository extends JpaRepository<User, Long> {
    /**
     * 多表查询
     * @param username
     * @return
     */
    @Query(nativeQuery = true, value ="SELECT u.id,r.value,u.username,u.password FROM t_user u,t_role r,t_user_role ur WHERE u.id=ur.user_id and r.id=ur.role_id and u.username=?1")
    List<Object[]> findByNameContaining(String username);

    //And用法
    User findById(long id);

    User findByIdAndUsername(long id, String username);

    User findByUsername(String username);
    //Or用法
    User findByIdOrUsername(long id, String name);

    //Between用法
    User findByCreateTimeBetween(Date start, Date end);

    //LessThan用法
    List<User> findByCreateTimeLessThan(Date start);

    //GreaterThan用法
    List<User> findByCreateTimeGreaterThan(Date start);

    //IsNull/IsNutNull用法
    List<User> findByUsernameIsNull();

    //Like/NotLike用法
    List<User> findByUsernameLike(String username);

    //OrderBy用法
    List<User> findByUsernameOrderByIdAsc(String username);

    //Not用法
    List<User> findByUsernameNot(String username);

    //In/NotIn用法
    List<User> findByUsernameIn(Collection<String> nameList);

}
